home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Textfiles / zines / DNA / DNAV1I4.sit / DNAV1I4 / ARTICLE.012 < prev    next >
Text File  |  1993-09-04  |  11KB  |  151 lines

  1.            x86 Assembly Language Tutorial [1/?] by Horsepowr
  2.                             Written for DnA
  3.  
  4.  
  5.        This article is the first in a series of documents of which aim to 
  6. teach the novice, or experienced high-level language programmer x86 assembly
  7. language.  As you all know, assembly language, or more appropriately, 
  8. representitive mnemonic code, is a symbolic form of the processor's host 
  9. machine language.  Every opcode (or function, if you will) is directly 
  10. represented in the cpu's microcode, which controls everything the cpu can do, 
  11. and is, in fact, the ONLY way the processor can "speak" with software.  No 
  12. matter what language is used, what operating system is being run, or anything, 
  13. all code is processed by the cpu as machine ocde, which as I've stated, is 
  14. just a direct binary translation of assembly language.  An assembler, is NOT a 
  15. compiler, a compiler translates a language to object code (we will get into
  16. this term in a later article), while an assembler simply checks to make sure
  17. all the instuctions are valid for the given processor, and converts it to 
  18. object code from there, with no "translation" like a compiler does.  This 
  19. results in executables that not only have FULL control over the entire system
  20. (potentially, as often times the operating system will restrict certain 
  21. processes), but have no extraneous code whatsoever.  The end result is code 
  22. that is 100% efficient (assuming the programmer optimized his/her routines
  23. during the coding process) and of the smallest possible size.  This is 
  24. valuable in any instance where size is of importance (low memory/disk space 
  25. situations), speed is needed (video, I/O, DMA) or direct control of the system
  26. level hardware is required (an operating system or device driver).
  27.         
  28.         Now that you understand what assembly language is, you may ask, why
  29. would you want to learn such a terse and cryptic language?  The answer is
  30. explained in the advantages of assembly language.  If a C program's video
  31. routines leave something to be desired, assembly language routines are often
  32. the very key to many succesful projects.  Anyone interested in Virii creation
  33. also will like the aspects of control and the minimal size offered by this
  34. machine level code.  You may also say: Yeah but isn't assembly language really
  35. hard to learn and doesn't it take forever to code in?  To answer the first
  36. topic: No, not if you have knowledge of the cpu you'll be coding in, or better
  37. yet, knowledge of programming on the platform of choice.  It's simply learning
  38. logic that is hard for many people.  To print to the screen, it's not just 
  39. some simple 1 line function.  You must interface with the hardware either 
  40. directly, or indirectly through the BIOS and/or operating system.  The coder
  41. (programmer) must then place all the instructions in just the right order, 
  42. with all the proper precautions taken for success.  This is gained through 
  43. experience with the language, as it is with any other language.  To answer
  44. the second question, yes, it does take more time to code in assembler, but the
  45. key is, you invest more time initially in the code, so that it will take LESS
  46. time to execute.  It's not reccomended that software be written in 100% 
  47. assembly, as that is often times foolish, but the proper combination of a high 
  48. level language that implements speed and/or size crucial assembly language
  49. routines with the main code is often the difference between a mediocre 
  50. program and one that has people sending you all sorts of mail about how
  51. bitchen your stuff is and how can they get more, etc, etc.
  52.         As the issues progress I will get more and more in-depth regarding
  53. coding in assembler, and those of you who are extremely application oriented
  54. will not feel patronized or deprived in this area, but for the first few 
  55. articles, we'll be looking at the basics.  Since you've already read thus far,
  56. I assume you are still interested in learning assembler.  The first thing any 
  57. person wanting to learn assembler needs to know is hexidecimal notation.  If
  58. you are an experienced C programmer, or an advanced BASIC or Pascal 
  59. programmer, you may want to bypass it, but if you feel at all unsure about 
  60. your knowledge on hex, then by all means, read on and refresh yourself.
  61.         Hexidecimal notation is merely an easy and efficient way of 
  62. representing binary numerals.  For example the hexidecimal number FF (yes I 
  63. realize they are letters, but they are representitive of numbers, so they
  64. are treated as such) is equeal to 11111111 in binary.  Isn't it much easier
  65. to read or type FF than 11111111?  And it gets worse as nubers grow, as FF is 
  66. merely the decimal equivilent of 255.  Imagine the complexity in binary of
  67. the decimal number 12,309,851!  Hexidecimal is by far the choice base for
  68. assembly language programming, and is therefore crucial that you understand 
  69. them, and are comfortable working with them.  First of all, if you are not 
  70. familiar with binary (base 2) I will explain that.  Binary is a numeric 
  71. system in which the only different digits that may be used in each position
  72. are a 0 or a 1.  This is great for logic and electronics as digits can be 
  73. represented by a true or false value (on/off in an electronic curcuit).  But
  74. it is rather limiting in the fact that as quantity's grow large, so do the 
  75. place values of the number.  The way the system works is like this:  Picture
  76. an egg carton, with only the bottom row of 6 egg holes left.  Let these holes 
  77. represent the #'s 1, 2, 4, 8, 16, and 32, from right to left (see diagram).
  78.  
  79.         32      16       8       4       2       1
  80.         ___     ___     ___     ___     ___     ___
  81.        /   \   /   \   /   \   /   \   /   \   /   \
  82.        | X |   |   |   | X |   | X |   | X |   |   |
  83.        \   /   \   /   \   /   \   /   \   /   \   /
  84.         ---     ---     ---     ---     ---     ---
  85.          1       0       1       1       1       0
  86.  
  87. You'll notice that in each egg pouch that has an "X" in it, the numeral 1
  88. is right below it, and those which are empty have zeros.  This is how binary
  89. works.  Starting from the right, each digit represents 2x the number 
  90. preceeding it, and the rightmost digit always represents the value 1.  To get
  91. a total value of a binary number, you add all the values for the 1 digits, so
  92. in the example, the sum of the values of the 1 digits would be 32+8+4+2, or
  93. 46.  So the binary number 101110 is equal to the decimal number 46.  
  94. Hexidecimal is merely a way of representing binary #'s, in effect condensing 
  95. them.  Each hexidecimal (which will me henceforth referred to as `hex') digit
  96. represents 4 binary digits.  Hex is base 16, so each value position (place) 
  97. can have a maximum value of 15, just as decimal (base 10) can have a maximum
  98. value of nine.  For example, when you add 2+9 in decimal, the largest that 
  99. the "ones" place can equal is 9 (0-9 equals 10 digits, hence base 10), you 
  100. must "carry" when the additive exceeds this maximum value, yielding 11, which 
  101. takes two digits.  When you add hex, you do not have to carry until the value 
  102. exceeds 15, but you may ask how can 15 be represented in a single digit?  The 
  103. answer is by letters.  As in decimal, 0-9 is equal to the values 0-9, but 
  104. rather than having to move over a value place, 9 increases to A, then B, etc, 
  105. all the way to F (which is equal to a decimal 15), at which it has reached 
  106. it's digit bound, and must carry to 10.  So if in hex the value doesn't carry 
  107. into the second place until the first digit exceeds 15, the 1 in 10 hex is 
  108. equal to 16.  So although you may see 10 as 10 decimal, 10 hex is actually 16, 
  109. meaning 16*1+0.  For each place in a hexidecimal number, you exponentialize 16 
  110. by it's distance form the first digit ( which in the case of 10, the distance 
  111. is 1 so 16 to the 1st power is 16) times the value of the digit. So if it was 
  112. 20 hex, you would say, okay the 2 is 1 distance away from the ones place, so 
  113. we multiply the 2 times 16 to the first, which is 32 decimal, plus anything in 
  114. the rightmost places, which in this case happen to be zero, so your total 
  115. is 32.  A 3 digit example is 2A7 hex.  The 2 is a distance of 2 from the ones 
  116. place, so 16 to the 2nd power is 256, which multiplied by two is 512.  The A
  117. is 1 digit away from the ones spot, so it will be 16 to the first power, or
  118. 16 times A, which is 10 decimal, so 160.  The ones spot contains 7, which is
  119. zero distance form the one's place, so 16 to the zero power is 1 so 7 times 1
  120. equals zero.  Then you add all these digit valuse together (512+160+7) to get
  121. 679 decimal.  Now that you understand how hex relates to decimal values (you
  122. do understand right, I haven't lost you yet have I?), it's much easier to see
  123. how hex relates to binary.  As I stated before, 1 hex digit (Maximum value of
  124. 15) represents 4 binary digits (1111 = 8+4+2+1 = 15, so once again, a maximum
  125. value of 15) it's just a matter of compressing 4 digits with a maximum value 
  126. of 15 to one digit with a maximum value of 15.  For example, 1001 binary is
  127. 8+0+0+1 or 9.  Nine in hex is just that, 9.  Okay, how about 10110110 binary?
  128. No problem, take them 4 bits (binary digits) at a  time for each hex digit.
  129. The first four are 1011 = 8+0+2+1 = 11 decimal = B hex, the second four are
  130. 0110 = 0+4+2+0 = 6 decimal = 6 hex, then put the two hex digits together to
  131. get the grand total of B6, which is equal to 10110110 binary which is equal
  132. to 182 decimal.  If this is really making no sense to you and you have taken
  133. at least a pre-collegiate algebra class or better, and have some knowledge
  134. of computer programming then I suggest getting Peter Norton's Book on 
  135. PC Assembly, which is not a very good source on learning assembly language, 
  136. but is very helpful in learning base conversions.  If you have less than an 
  137. academic level of math, and have no programming experience, it is 
  138. understandable that you are confused.  It is advised to you at this point to 
  139. seek a language with a hardware shelter such as BASIC or Pascal.  If you
  140. did grasp this concept, Great!, you're well on your way to learing assembly
  141. language.
  142.         This concludes this article, as your eye's are almost as tired as my 
  143. fingers, but look for the next article where I will discuss the way the x86
  144. processors handle data and instructions using memory and the registers.  If
  145. you have any questions, feel free to channel any feedback, requests, hate
  146. mail, or whatever through DnA, or direct to me at my system: The Finish Line
  147. (714) 572-8696 v.32bis.
  148.  
  149.                 Hasta -HP       ƒƒƒtURB@ƒƒƒ
  150.  
  151.